<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>代付管理</cite></a>
        <a><cite>代付通道</cite></a>
        <a><cite>子账户</cite></a>
    </span>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">子账户列表</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-row">
                    <div class="layuiAdmin-btns" style="margin-bottom: 10px;">
                        <a class="layui-btn create" data-type="all" data-events="create"><i class="layui-icon layui-icon-add-1"></i>新增账户</a>
                    </div>
                </div>
                <table id="cList" lay-filter="cList"></table>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['table','util', 'form', 'admin'], function(){
        var table = layui.table
        ,$ = layui.$
        , admin = layui.admin
        ,form = layui.form
        ,view = layui.view
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

        var router = layui.router();
        var agentpayPassageId = router.search.agentpayPassageId;


        var tplEdit = function(d){
            return "<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='risk'><i class='layui-icon layui-icon-engine'></i>风控</a>" +
                    /*"<a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='balance'>余额</a>" +*/
                    "<a class='layui-btn layui-btn-xs' lay-event='edit'><i class='layui-icon layui-icon-edit'></i>编辑</a>";
        };

        var tplStatus = function(d){
            var checkedHtml = " id='"+d.id+"' ";
            if(d.status == 1) {
                checkedHtml += " checked";
            }
            return '<input type="checkbox" lay-filter="statusSwitch" lay-skin="switch" '+checkedHtml+' lay-text="启用|停用">';
        };

        table.render({
            elem: '#cList'
            ,url: layui.setter.baseUrl + '/config/agentpay_passage_account/list'
            ,where: { access_token: layui.data(layui.setter.tableName).access_token, agentpayPassageId: agentpayPassageId}
            ,id: 'tableReload'
            ,cols: [[
                {field:'id', title: '账户ID'}
                ,{field:'accountName', title: '账户名称'}
                ,{field:'ifCode', title: '接口代码'}
                ,{field:'ifName', title: '接口名称'}
                ,{field:'ifTypeCode', title: '接口类型代码'}
                ,{field:'ifTypeName', title: '接口类型名称'}
                ,{field:'balance', title: '余额'}
                ,{field:'status', title: '账户状态',templet: tplStatus }
                ,{field:'edite', width: 260, title: '操作', fixed: 'right', templet: tplEdit }
            ]]
            ,page: true
            ,skin: 'line'
        });

        //监听工具条
        table.on('tool(cList)', function(obj){
            var data = obj.data;
            if(obj.event === 'risk'){
                view.xxpayPopup("风控", "agentpay/agentpay_passage_account/risk_set", {id: data.id}, {area:['80%','70%']});
            } else if(obj.event === 'edit'){

                view.xxpayPopup("编辑", "agentpay/agentpay_passage_account/update", {id: data.id}, {area:['90%','80%']});

            } else if(obj.event == 'balance') {
                layer.msg('正在查询账户余额...', {icon: 16});
                admin.req({
                    type: 'post',
                    url: layui.setter.baseUrl + '/config/agentpay_passage_account/balance',
                    data: {
                        id: data.id
                    },
                    error: function(err){
                        layer.alert(err);
                    },
                    success: function(res){
                        layer.closeAll('loading');
                        if(res.code == 0){
                            layer.open({
                                title: '账户' + data.accountName + '余额'
                                ,content: res.data
                            });
                        }
                    }
                });
            }
        });

        $('.create').click(function(){
            view.xxpayPopup("新增账户", "agentpay/agentpay_passage_account/create", {agentpayPassageId: agentpayPassageId}, {area:['90%','80%']});
        });


        form.on('switch(statusSwitch)', function(data){
            var id = $(data.elem).attr('id');
            var updateState = data.elem.checked ? "1" : "0";
            var loadingIndex = layer.load();
            admin.req({
                type: "POST",
                url: layui.setter.baseUrl + '/config/agentpay_passage_account/update',
                data: {id: id, status: updateState},
                success: function(res){
                    if(res.code == 0) {
                        table.reload('tableReload');
                    }
                },
                complete:function(){
                    layer.close(loadingIndex);
                }
            });
        });

        // 渲染表单
        form.render();

    });
    
</script>
